~ chicken-core (master) /manual/Module (chicken number-vector)


  1[[tags: manual]]
  2[[toc:]]
  3
  4== Module (chicken numvector)
  5
  6Homogeneous numeric vector datatypes. This module provides a superset
  7of [[http://srfi.schemers.org/srfi-4/srfi-4.html|SRFI-4]]. The 
  8module [[Module srfi-4|srfi-4]] is also available for compatibility reasons.
  9
 10When loaded, the feature identifier {{srfi-4}} is defined.
 11
 12=== CHICKEN implementation specifics and extensions to SRFI-4
 13
 14* Procedures for [[Module (chicken bytevector)|bytevector]] conversion, subvectors and vector I/O are provided.
 15* SRFI-17 setters for {{XXXvector-ref}} are defined.
 16* Constructors allow allocating the storage in non garbage collected memory.
 17* Vectors for 64 and 128-bit complex numbers are provided.
 18
 19This module provides a set of datatypes for vectors whose elements are
 20of the same numeric type (signed or unsigned exact integer or inexact
 21real of a given precision). These datatypes support operations analogous
 22to the Scheme vector type, but they are distinct datatypes. An external
 23representation is specified which must be supported by the {{read}} and
 24{{write}} procedures and by the program parser (i.e. programs can contain
 25references to literal homogeneous vectors).
 26
 27=== Datatypes
 28
 29There are 8 datatypes of exact integer homogeneous vectors (which will be
 30called integer vectors):
 31
 32<table>
 33<tr><th>Datatype</th><th>Type of elements</th></tr>
 34<tr><td>{{s8vector}}</td><td>signed exact integer in the range -(2^7) to (2^7)-1</td></tr>
 35<tr><td>{{u8vector}}</td><td>unsigned exact integer in the range 0 to (2^8)-1</td></tr>
 36<tr><td>{{s16vector}}</td><td>signed exact integer in the range -(2^15) to (2^15)-1</td></tr>
 37<tr><td>{{u16vector}}</td><td>unsigned exact integer in the range 0 to (2^16)-1</td></tr>
 38<tr><td>{{s32vector}}</td><td>signed exact integer in the range -(2^31) to (2^31)-1</td></tr>
 39<tr><td>{{u32vector}}</td><td>unsigned exact integer in the range 0 to (2^32)-1</td></tr>
 40<tr><td>{{s64vector}}</td><td>signed exact integer in the range -(2^31) to (2^31)-1</td></tr>
 41<tr><td>{{u64vector}}</td><td>unsigned exact integer in the range 0 to (2^64)-1</td></tr>
 42<tr><td>{{s64vector}}</td><td>signed exact integer in the range -(2^63) to (2^63)-1</td></tr>
 43<tr><td>{{u64vector}}</td><td>unsigned exact integer in the range 0 to (2^64)-1</td></tr></table>
 44
 45There are 2 datatypes of inexact real homogeneous vectors (which will be
 46called float vectors):
 47
 48<table>
 49<tr><th>Datatype</th><th>Type of elements</th></tr>
 50<tr><td>{{f32vector}}</td><td>inexact real</td></tr>
 51<tr><td>{{f64vector}}</td><td>inexact real</td></tr></table>
 52
 53The only difference between the two float vector types is that
 54{{f64vector}}s preserve at least as much precision as {{f32vector}}s.
 55
 56And there are two datatypes of inexact complex homogeneous vectors (which will be called complex vectors): 
 57
 58<table>
 59<tr><th>Datatype</th><th>Type of elements</th></tr>
 60<tr><td>{{c64vector}}</th><th>inexact complex, typically 64 bits</td></tr>
 61<tr><td>{{c128vector}}</th><th>inexact complex, typically 128 bits</td></tr>
 62</table>
 63
 64Each homogeneous vector datatype has an external representation which
 65is supported by the {{read}} and {{write}} procedures and by the program
 66parser. Each datatype also has a set of associated predefined procedures
 67analogous to those available for Scheme's heterogeneous vectors.
 68
 69=== External representation
 70
 71<read>#u8</read><br>
 72<read>#u16</read><br>
 73<read>#u32</read><br>
 74<read>#s8</read><br>
 75<read>#s16</read><br>
 76<read>#s32</read><br>
 77<read>#f32</read><br>
 78<read>#f64</read><br>
 79<read>#c64</read><br>
 80<read>#c128</read><br>
 81
 82The external representation of instances of the datatype {{XXXvector}}
 83is {{#XXX( ...elements... )}}.
 84
 85For example, 
 86
 87 #u8(0 #e1e2 #xff)}}  ; a {{u8vector}} of length 3 containing 0, 100, 255
 88 #f64(-1.5)           ; a {{f64vector}} of length 1 containing -1.5.
 89
 90This external representation is also available in program source code. For example, 
 91
 92 (set! x '#u8(1 2 3))
 93
 94will set {{x}} to the object {{#u8(1 2 3)}}. Since CHICKEN 4.9.0, literal homogeneous vectors do not have to be quoted. Homogeneous vectors can appear in quasiquotations but must not contain {{unquote}} or {{unquote-splicing}} forms.  ''I.e.'',
 95
 96 `(,x #u8(1 2))        ; legal
 97 `#u8(1 ,x 2)          ; illegal
 98
 99Elements may also be characters or strings, in that case they are interpreted
100as a sequence of numerical character codes. For example,
101
102 '#u8(#\x7f "EL" #\F 2 1)
103
104is equivalent to
105
106 '#u8(#\x7f #\x45 #\x4c #\x46 2 1)
107
108Character literals inside numeric vectors expand into the UTF-8 sequence of
109the characters they represent, for strings the contained characters 
110are interpreted in whatever encoding is used for the text file or stream 
111in which the literal appears.
112
113Note that {{#u8"..."}} can be used as an abbreviation for the special case
114{{#u8("...")}}.
115
116=== Predicates
117
118<procedure>(u8vector? OBJ)</procedure><br>
119<procedure>(s8vector? OBJ)</procedure><br>
120<procedure>(u16vector? OBJ)</procedure><br>
121<procedure>(s16vector? OBJ)</procedure><br>
122<procedure>(u32vector? OBJ)</procedure><br>
123<procedure>(s32vector? OBJ)</procedure><br>
124<procedure>(u64vector? OBJ)</procedure><br>
125<procedure>(s64vector? OBJ)</procedure><br>
126<procedure>(f32vector? OBJ)</procedure><br>
127<procedure>(f64vector? OBJ)</procedure><br>
128<procedure>(c64vector? OBJ)</procedure><br>
129<procedure>(c128vector? OBJ)</procedure><br>
130
131Return {{#t}} if {{obj}} is an object of the specified type or {{#f}} if not.
132
133<procedure>(number-vector? OBJ)</procedure>
134
135Return {{#t}} if {{obj}} is a number vector, {{#f}} if not.  A "number vector" is any of the homogeneous number vector types defined by SRFI-4, ie it's one of {{u8vector}}, {{s8vector}}, {{u16vector}}, {{s16vector}}, {{u32vector}}, {{s32vector}}, {{u64vector}}, {{s64vector}}, {{f32vector}}, {{f64vector}}, {{c64vector}} or {{c128vector}}.
136
137
138=== Constructors
139
140<procedure>(make-u8vector N [U8VALUE NONGC FINALIZE])</procedure><br>
141<procedure>(make-s8vector N [S8VALUE NONGC FINALIZE])</procedure><br>
142<procedure>(make-u16vector N [U16VALUE NONGC FINALIZE])</procedure><br>
143<procedure>(make-s16vector N [S16VALUE NONGC FINALIZE])</procedure><br>
144<procedure>(make-u32vector N [U32VALUE NONGC FINALIZE])</procedure><br>
145<procedure>(make-s32vector N [S32VALUE NONGC FINALIZE])</procedure><br>
146<procedure>(make-u64vector N [U64VALUE NONGC FINALIZE])</procedure><br>
147<procedure>(make-s64vector N [S64VALUE NONGC FINALIZE])</procedure><br>
148<procedure>(make-f32vector N [F32VALUE NONGC FINALIZE])</procedure><br>
149<procedure>(make-f64vector N [F64VALUE NONGC FINALIZE])</procedure><br>
150<procedure>(make-c64vector N [C64VALUE NONGC FINALIZE])</procedure><br>
151<procedure>(make-c128vector N [C128VALUE NONGC FINALIZE])</procedure><br>
152
153Return a newly-allocated SRFI-4 homogeneous number vector of length N.
154
155If the optional fill VALUE is specified, it specifies the initial
156value for each slot in the vector.  If not, the content of the vector
157is unspecified but individual elements of the vector are guaranteed to
158be in the range of values permitted for that type of vector.
159
160The type of the fill value must be compatible with the elements of the
161vector datatype.  It is an error if otherwise -- for example, if an
162inexact integer is passed to {{make-u8vector}}.
163
164On CHICKEN, these procedures have been extended to allow allocating
165the storage in non-garbage collected memory, as follows:
166
167The optional arguments {{NONGC}} and {{FINALIZE}} define whether the
168vector should be allocated in a memory area not subject to garbage
169collection and whether the associated storage should be automatically
170freed (using finalization) when there are no references from Scheme
171variables and data.  {{NONGC}} defaults to {{#f}} (the vector will be
172located in normal garbage collected memory) and {{FINALIZE}} defaults
173to {{#t}}. Note that the {{FINALIZE}} argument is only used when
174{{NONGC}} is true.
175
176<procedure>(u8vector U8VALUE ...)</procedure><br>
177<procedure>(s8vector S8VALUE ...)</procedure><br>
178<procedure>(u16vector U16VALUE ...)</procedure><br>
179<procedure>(s16vector S16VALUE ...)</procedure><br>
180<procedure>(u32vector U32VALUE ...)</procedure><br>
181<procedure>(s32vector S32VALUE ...)</procedure><br>
182<procedure>(u64vector U64VALUE ...)</procedure><br>
183<procedure>(s64vector S64VALUE ...)</procedure><br>
184<procedure>(f32vector F32VALUE ...)</procedure><br>
185<procedure>(f64vector F64VALUE ...)</procedure><br>
186<procedure>(c64vector C64VALUE ...)</procedure><br>
187<procedure>(c128vector C128VALUE ...)</procedure><br>
188
189Return a newly-allocated SRFI-4 homogeneous number vector of the specified
190type, composed of the arguments.
191
192=== Length
193
194<procedure>(u8vector-length U8VECTOR)</procedure><br>
195<procedure>(s8vector-length S8VECTOR)</procedure><br>
196<procedure>(u16vector-length U16VECTOR)</procedure><br>
197<procedure>(s16vector-length S16VECTOR)</procedure><br>
198<procedure>(u32vector-length U32VECTOR)</procedure><br>
199<procedure>(s32vector-length S32VECTOR)</procedure><br>
200<procedure>(u64vector-length U64VECTOR)</procedure><br>
201<procedure>(s64vector-length S64VECTOR)</procedure><br>
202<procedure>(f32vector-length F32VECTOR)</procedure><br>
203<procedure>(f64vector-length F64VECTOR)</procedure><br>
204<procedure>(c64vector-length C64VECTOR)</procedure><br>
205<procedure>(c128vector-length C128VECTOR)</procedure><br>
206
207Returns the length of the SRFI-4 homogeneous number VECTOR.
208
209=== Getters
210
211<procedure>(u8vector-ref U8VECTOR I)</procedure><br>
212<procedure>(s8vector-ref S8VECTOR i)</procedure><br>
213<procedure>(u16vector-ref U16VECTOR I)</procedure><br>
214<procedure>(s16vector-ref S16VECTOR I)</procedure><br>
215<procedure>(u32vector-ref U32VECTOR I)</procedure><br>
216<procedure>(s32vector-ref S32VECTOR I)</procedure><br>
217<procedure>(u64vector-ref U64VECTOR I)</procedure><br>
218<procedure>(s64vector-ref S64VECTOR I)</procedure><br>
219<procedure>(f32vector-ref F32VECTOR I)</procedure><br>
220<procedure>(f64vector-ref F64VECTOR I)</procedure><br>
221<procedure>(c64vector-ref C64VECTOR I)</procedure><br>
222<procedure>(c128vector-ref C128VECTOR I)</procedure><br>
223
224Return the value of the ''i''th element of the SRFI-4 homogeneous
225number vector, where {{I}} is a nonnegative exact integer less
226than the length of the vector.
227
228=== Setters
229
230<procedure>(u8vector-set! U8VECTOR I U8VALUE)</procedure><br>
231<procedure>(s8vector-set! S8VECTOR I S8VALUE)</procedure><br>
232<procedure>(u16vector-set! U16VECTOR I U16VALUE)</procedure><br>
233<procedure>(s16vector-set! S16VECTOR I S16VALUE)</procedure><br>
234<procedure>(u32vector-set! U32VECTOR I U32VALUE)</procedure><br>
235<procedure>(s32vector-set! S32VECTOR I S32VALUE)</procedure><br>
236<procedure>(u64vector-set! U64VECTOR I U64VALUE)</procedure><br>
237<procedure>(s64vector-set! S64VECTOR I S64VALUE)</procedure><br>
238<procedure>(f32vector-set! F32VECTOR I F32VALUE)</procedure><br>
239<procedure>(f64vector-set! F64VECTOR I F64VALUE)</procedure><br>
240<procedure>(c64vector-set! C64VECTOR I C64VALUE)</procedure><br>
241<procedure>(c128vector-set! C128VECTOR I C128VALUE)</procedure><br>
242
243Set the {{i}}th element of the SRFI-4 homogeneous number VECTOR to
244VALUE.  {{I}} is a nonnegative exact integer less than the length of
245the vector and VALUE must be the same type as the elements of the
246vector datatype.
247
248Additionally, SRFI-17 setters are defined on all {{xxxvector-ref}}
249procedures.  For example, to set the {{i}}th element of SRFI-4
250{{u8vector}} to {{u8value}}:
251
252 (set! (u8vector-ref u8vector i) u8value)
253
254=== Conversions
255
256<procedure>(u8vector->list U8VECTOR)</procedure><br>
257<procedure>(s8vector->list S8VECTOR)</procedure><br>
258<procedure>(u16vector->list U16VECTOR)</procedure><br>
259<procedure>(s16vector->list S16VECTOR)</procedure><br>
260<procedure>(u32vector->list U32VECTOR)</procedure><br>
261<procedure>(s32vector->list S32VECTOR)</procedure><br>
262<procedure>(u64vector->list U64VECTOR)</procedure><br>
263<procedure>(s64vector->list S64VECTOR)</procedure><br>
264<procedure>(f32vector->list F32VECTOR)</procedure><br>
265<procedure>(f64vector->list F64VECTOR)</procedure><br>
266<procedure>(c64vector->list C64VECTOR)</procedure><br>
267<procedure>(c128vector->list C128VECTOR)</procedure><br>
268
269Return a list consisting of the elements of SRFI-4 homogeneous number
270VECTOR.
271
272<procedure>(list->u8vector U8LIST)</procedure><br>
273<procedure>(list->s8vector S8LIST)</procedure><br>
274<procedure>(list->u16vector U16LIST)</procedure><br>
275<procedure>(list->s16vector S16LIST)</procedure><br>
276<procedure>(list->u32vector U32LIST)</procedure><br>
277<procedure>(list->s32vector S32LIST)</procedure><br>
278<procedure>(list->u64vector U64LIST)</procedure><br>
279<procedure>(list->s64vector S64LIST)</procedure><br>
280<procedure>(list->f32vector F32LIST)</procedure><br>
281<procedure>(list->f64vector F64LIST)</procedure><br>
282<procedure>(list->c64vector C64LIST)</procedure><br>
283<procedure>(list->c128vector C128LIST)</procedure><br>
284
285Return a newly-allocated SRFI-4 homogeneous number VECTOR consisting
286of the elements of LIST.  Each element of LIST must be compatible
287with the datatype of VECTOR.
288
289
290=== Blob conversions
291
292As a number vector is basically just a [[Module (chicken bytevector)|bytevector]]
293wrapped into a record type,
294there are several procedures which can convert between bytevectors and
295number vectors.
296
297Note that built-in bytevectors are identical to u8vectors.
298
299<procedure>(s8vector->bytevector S8VECTOR)</procedure><br>
300<procedure>(u16vector->bytevector U16VECTOR)</procedure><br>
301<procedure>(s16vector->bytevector S16VECTOR)</procedure><br>
302<procedure>(u32vector->bytevector U32VECTOR)</procedure><br>
303<procedure>(s32vector->bytevector S32VECTOR)</procedure><br>
304<procedure>(u64vector->bytevector U64VECTOR)</procedure><br>
305<procedure>(s64vector->bytevector S64VECTOR)</procedure><br>
306<procedure>(f32vector->bytevector F32VECTOR)</procedure><br>
307<procedure>(f64vector->bytevector F64VECTOR)</procedure><br>
308<procedure>(c64vector->bytevector C64VECTOR)</procedure><br>
309<procedure>(c128vector->bytevector C128VECTOR)</procedure><br>
310<procedure>(u8vector->bytevector/shared U8VECTOR)</procedure><br>
311<procedure>(s8vector->bytevector/shared S8VECTOR)</procedure><br>
312<procedure>(u16vector->bytevector/shared U16VECTOR)</procedure><br>
313<procedure>(s16vector->bytevector/shared S16VECTOR)</procedure><br>
314<procedure>(u32vector->bytevector/shared U32VECTOR)</procedure><br>
315<procedure>(s32vector->bytevector/shared S32VECTOR)</procedure><br>
316<procedure>(u64vector->bytevector/shared U64VECTOR)</procedure><br>
317<procedure>(s64vector->bytevector/shared S64VECTOR)</procedure><br>
318<procedure>(f32vector->bytevector/shared F32VECTOR)</procedure><br>
319<procedure>(f64vector->bytevector/shared F64VECTOR)</procedure><br>
320<procedure>(c64vector->bytevector/shared C64VECTOR)</procedure><br>
321<procedure>(c128vector->bytevector/shared C128VECTOR)</procedure><br>
322
323Each of these procedures return the contents of the given vector as a
324'packed' bytevector. The byte order in that vector is platform-dependent
325(for example little-endian on an '''Intel''' processor). The
326{{/shared}} variants return a bytevector that shares memory with the
327contents of the vector, the others will copy the contents of the
328vector's internal bytevector object.
329
330<procedure>(bytevector->s8vector BYTEVECTOR)</procedure><br>
331<procedure>(bytevector->u16vector BYTEVECTOR)</procedure><br>
332<procedure>(bytevector->s16vector BYTEVECTOR)</procedure><br>
333<procedure>(bytevector->u32vector BYTEVECTOR)</procedure><br>
334<procedure>(bytevector->s32vector BYTEVECTOR)</procedure><br>
335<procedure>(bytevector->u64vector BYTEVECTOR)</procedure><br>
336<procedure>(bytevector->s64vector BYTEVECTOR)</procedure><br>
337<procedure>(bytevector->f32vector BYTEVECTOR)</procedure><br>
338<procedure>(bytevector->f64vector BYTEVECTOR)</procedure><br>
339<procedure>(bytevector->c64vector BYTEVECTOR)</procedure><br>
340<procedure>(bytevector->c128vector BYTEVECTOR)</procedure><br>
341<procedure>(bytevector->s8vector/shared BYTEVECTOR)</procedure><br>
342<procedure>(bytevector->u16vector/shared BYTEVECTOR)</procedure><br>
343<procedure>(bytevector->s16vector/shared BYTEVECTOR)</procedure><br>
344<procedure>(bytevector->u32vector/shared BYTEVECTOR)</procedure><br>
345<procedure>(bytevector->s32vector/shared BYTEVECTOR)</procedure><br>
346<procedure>(bytevector->u64vector/shared BYTEVECTOR)</procedure><br>
347<procedure>(bytevector->s64vector/shared BYTEVECTOR)</procedure><br>
348<procedure>(bytevector->f32vector/shared BYTEVECTOR)</procedure><br>
349<procedure>(bytevector->f64vector/shared BYTEVECTOR)</procedure><br>
350<procedure>(bytevector->c64vector/shared BYTEVECTOR)</procedure><br>
351<procedure>(bytevector->c128vector/shared BYTEVECTOR)</procedure><br>
352
353Each of these procedures return a vector where the argument {{BYTEVECTOR}}
354is taken as a 'packed' representation of the contents of the
355vector. The {{/shared}} variants return a vector that shares memory
356with the contents of the bytevector, the others will copy the bytevector.
357
358=== Subvectors
359
360<procedure>(subu8vector U8VECTOR FROM TO)</procedure><br>
361<procedure>(subu16vector U16VECTOR FROM TO)</procedure><br>
362<procedure>(subu32vector U32VECTOR FROM TO)</procedure><br>
363<procedure>(subu64vector U32VECTOR FROM TO)</procedure><br>
364<procedure>(subs8vector S8VECTOR FROM TO)</procedure><br>
365<procedure>(subs16vector S16VECTOR FROM TO)</procedure><br>
366<procedure>(subs32vector S32VECTOR FROM TO)</procedure><br>
367<procedure>(subs64vector S32VECTOR FROM TO)</procedure><br>
368<procedure>(subf32vector F32VECTOR FROM TO)</procedure><br>
369<procedure>(subf64vector F64VECTOR FROM TO)</procedure><br>
370<procedure>(subc64vector C64VECTOR FROM TO)</procedure><br>
371<procedure>(subc128vector C128VECTOR FROM TO)</procedure><br>
372
373Creates a fresh number vector of the same type as the argument vector
374with the elements at the positions {{FROM}} up to but not including
375{{TO}}.
376
377=== Release number vectors allocated in static memory
378
379<procedure>(release-number-vector NVECTOR)</procedure>
380
381Release the storage of a SRFI-4 vector that was allocated in
382non-garbage collected memory (for example using the {{NONGC}} argument
383for one of the {{make-XXXvector}} constructor procedures). The effect
384of calling this procedure with a number vector allocated in normal
385garbage collected memory is undefined.
386
387
388---
389Previous: [[Module (chicken type)]]
390
391Next: [[Interface to external functions and variables]]
392
Trap